<?php
/**
 * @package panel
 * @subpackage panel.environment.db
 */
/**
 * Raw select object
 * @author Dmitry Bakaleinik (code)
 */
class panel__db__raw_select {

	protected $sql;
	protected $iud;

	/**
	 * @return panel__db__raw_select
	 */
	static public function create($_sql_ = null, $_iud_ = FALSE) {

		$i = new self;

		if( $_sql_ ) {
			$i->setSQL($_sql_, $_iud_); }

		return $i;

	}	# Function

	public function setSQL($_, $_iud_ = FALSE) {

		$this->sql = $_;
		$this->iud = $_iud_;

		return $this;

	} 	# Function

	public function getSQL() {

		return $this->sql;

	}	# Function

	public function execute() {

		if( !$this->iud ) {

			return p()->db()->querySelect($this->getSQL());

		} else {

			return p()->db()->queryIUD($this->getSQL());

		}	// if

	}	# Function

	public function extractValue() {

		$r = self::execute();
		$a = func_get_args();

		array_unshift($a, $r);

		return call_user_func_array( array("maf__helpers__array", "getElement"), $a );

	}	# Function

	public function extractColumn($_column_) {

		$r = self::execute();
		return maf__helpers__array::extractColumn($r, $_column_);

	}	# Function

	public function extractFirst() {

		$r = self::execute();
		return current($r);

	}	# Function

}	# Class

/* EOF */